<?php
namespace app\controller\dataflow;


use app\validate\SimBindDeviceValidate;
use share\service\SimService;
use share\service\Factory4gManagerTableService;
use share\service\SimBindDeviceService;
use Exception;
use share\utils\Helper;

class SimController extends DataflowBaseController{    

    protected $beforeActionList = [
        "checkSign",
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){
        $this->validate = new SimBindDeviceValidate();
    }

    /**
     * 查询卡信息
     */
    public function info_action(){
        $input = $this->check();        
        $data = SimBindDeviceService::getBindInfo($input['iccid']);    
        $resp = self::formatOutput($data,$input);
        return self::ok($resp);

    }

    /**
     * 修改绑定信息
     */
    public function bind_update_action(){
        $input = $this->check();
        $data = SimBindDeviceService::bindSerialNo($input);
        $resp = self::formatOutput($data,$input);
        return self::ok($resp);   
    }

    /**
     * sim与设备建立映射关系
     */
    public function make_relation_action(){
        $input = $this->check();     
        $data = SimBindDeviceService::makeRelation($input);
        $resp = self::formatOutput($data,$input);
        return self::ok($resp);
    }


    public static function formatOutput($data,$origin = []){
        $res = [
            'id' => 0,
            'iccid' => isset($origin['iccid']) ? $origin['iccid'] : '',
            'netType' => '',
            'factoryId' => '',
            'factoryName' => '',
            'own' => 0,
            'locked' => 0,
            'bindStatus' => 0,
            'serialNo' => isset($origin['serialNo']) ? $origin['serialNo'] : '',
        ];
        if(!empty($data)){
            $res['id'] = $data['id'];
            $res['factoryId'] = $data['factory_id'];
            $res['factoryName'] = $data['factory_name'];
            $res['locked'] = $data['locked'];
            $res['serialNo'] = $data['serial_no'];
            $res['own'] = Factory4gManagerTableService::isOwnFactoryId($data['factory_id']) ? 1 : 0;
            $res['bindStatus'] = $data['status'] == 1 ? 1 : 0;            
            $res['netType'] = SimService::getNetTypeName($data['net_type']);
        }
        return $res;
    }
}